//
//  NSKCellModel.swift
//  NSKCellModel
//
//  Created by Fineland on 2021/3/8.
//
import UIKit

struct NSKCellModel: Codable {
    
    /// key值
    var valueKey: String!
    /// 对应的code-Key
    var codeKey: String?
    /// 名称
    var name: String!
    /// 副标题
    var subTitle: String?
    /// cell类型
    var cellType: String?
    /// 图标
    var tagImage: String?
    /// placeholder
    var placeholder: String?
    /// 是否为空？
    var notnull: Int?
    /// 键盘类型
    var keyboard: String?
    /// 规则
    var regular: String?
    /// 规则错误信息
    var rerrmsg: String?
    /// 是否显示Indicator？
    var showIndicator: Int?
    /// 是否可编辑？
    var canEdit: Int?
    /// 下一步vc标志
    var nextClass: String?
    /// 最大长度
    var maxLength: Int?
    /// 单位
    var suffix: String?
    /// 右侧图标
    var suffixIcon: String?
    /// 名称宽度
    var widthName: CGFloat?
    /// 下拉/选择列表
    var picker: Array<NSKPickerModel>?
    /// 值颜色
    var valueColor: String?
}

// MARK: -

struct NSKCellValueModel {
    
    /// 值
    var value: String?
    /// 原值
    var originValue: String?
    /// 选择值对应代码
    var code: String?
    /// 选择索引
    var selectedIndex: Int?
}

// MARK: -

struct NSKPickerModel: Codable {
    
    /// 名称
    var name: String!
    /// 对应代码
    var code: String?
    /// 别名
    var alaisName: String?
}

// MARK: -

extension UIViewController {
    
    func isNext(to data: Array<Array<NSKCellModel>>, in values: Dictionary<String, NSKCellValueModel>) -> (res: Bool, msg: String) {
        
        //检验是否成功？
        for i in data {
            for j in i {
                if j.notnull ?? 0 < 1 {
                    continue
                }
                //值是否为空？
                let tempVal = values[j.valueKey]?.value ?? ""
                if "" == tempVal {
                    return (false, j.placeholder ?? "")
                }
                //规则，P-手机号
                switch (j.regular ?? "") {
                case "P":
                    let regularVal = tempVal.regularPhone()
                    if true != regularVal {
                        return (false,  j.rerrmsg ?? "")
                    }
                default:
                    break
                }
           }
        }
        //返回
        return (true, "")
    }
    
    func values(to data: Array<Array<NSKCellModel>>, in values: Dictionary<String, Any>) -> Dictionary<String, NSKCellValueModel> {
        
        //结果
        var resultData: Dictionary<String, NSKCellValueModel> = [:]
        for i in data {
            for j in i {
                //当前值
                var tempObj: NSKCellValueModel!
                var tempVal = "-"
                if let valObj = values[j.valueKey] {
                    tempVal = "\(valObj)"
                    //是否有picker对象？如果存在，则需要将代码转成值
                    if let pickerObj = j.picker {
                        for picker in pickerObj.enumerated() where picker.element.code == tempVal {
                            tempObj = NSKCellValueModel(value: picker.element.name, code: tempVal, selectedIndex: picker.offset)
                            break
                        }
                    }
                }
                //是否有值？
                if nil == tempObj {
                    tempObj = NSKCellValueModel(value: tempVal)
                }
                resultData[j.valueKey] = tempObj
            }
        }
        //返回
        return resultData
    }
    
    func requestValus(to data: Array<Array<NSKCellModel>>, in values: Dictionary<String, NSKCellValueModel>) -> Dictionary<String, Any> {
        
        //结果
        var resultData: Dictionary<String, Any> = [:]
        for i in data {
            for j in i {
                //当前值对象
                guard let tempObj: NSKCellValueModel = values[j.valueKey] else {
                    continue
                }
                //注：这里code优先，如果有code，就不传code对门应的名称
                //有没有code值?
                if let codeKey = j.codeKey, let codeVal = tempObj.code {
                    resultData[codeKey] = codeVal
                    continue
                }
                if let tempVal = tempObj.value {
                    resultData[j.valueKey] = tempVal
                }
            }
        }
        //返回
        return resultData
    }
}
